/*************************************************************************
 > File Name   : restparser.js
 > Author      : zhoutk
 > Mail        : zhoutk@189.cn
 > Created Time: 2015-05-28 20:07
 ************************************************************************/

exports.parse = function(input){
    if(null == input || '' == input) return {};
    //去除URL末端的斜杠
    var str = removeSlashAtEnd(input),
        resIndex = str.indexOf('rs');
    if(resIndex == -1 || resIndex == str.length -2) return {};

    var queryStrs = str.substr(resIndex + 3).split('/');
    // id = 0 means list all children
    if(queryStrs.length % 2 != 0){
        queryStrs.push('0');
    }
    return  {
        resource : queryStrs[0],
        id : queryStrs[1]
    };
};

function removeSlashAtEnd(str){
    if(str.charAt(str.length -1) == '/'){
        return str.substring(0, str.length -1);
    }
    return str;
}